package com.woniuxy.woniubook.uitl;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

    //把Cookie写到 客户端
    public static void addCookie(HttpServletRequest request,HttpServletResponse response,  String token){
        Cookie c = new Cookie("user_token",token);
        //c.setDomain(""); //域名
        c.setPath("/"); //
        response.addCookie(c);
    }
    //把token从Cookie获取到
    public static String getToken(HttpServletRequest request,String cookieName){
        Cookie[] cookies = request.getCookies();//获取所有的cookie
        if (cookies != null && cookies.length > 0){
            for(Cookie c :cookies){
                if (c.getName().equals(cookieName)) {
                    return c.getValue();
                }
            }
        }
        return null;
    }


}
